package com.jbp.controller;

import com.jbp.model.Article;
import com.jbp.model.ArticleComment;
import com.jbp.service.ArticleCommentService;
import com.jbp.service.KeywordService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/**
 * 动态评论(ArticleComment)表控制层
 * @author cc
 * @since 2020-08-25 14:37:34
 */
@Api(tags ="APP接口--动态-评论" )
@RestController
@ApiIgnore
@RequestMapping("rest/articleComment")
public class ArticleCommentController {
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private KeywordService keywordService;
    /** 列表 */
    @ApiOperation(value="列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = ArticleComment.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId",value = "动态id",paramType = "body"),
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = articleCommentService.queryPage(params);
        return R.ok().put("page", page);
    }
    
    /** 保存 */
    @ApiOperation(value="保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId",value = "动态id",paramType = "body"),
            @ApiImplicitParam(name = "type",value = "类型 word 文本， image 图片， video 视频， voice 声音",paramType = "body"),
            @ApiImplicitParam(name = "content",value = "文本",paramType = "body"),
            @ApiImplicitParam(name = "files",value = "文件列表，多个文件用,分隔",paramType = "body"),
    })
    @PostMapping("/save")
    public R save(@RequestBody ArticleComment articleComment, @RequestAttribute("userId") Integer userId){
        articleComment.setReaderId(userId);
        keywordService.checkContent(articleComment.getContent());
        articleCommentService.add(articleComment);
        return R.ok(articleComment);
    }

    /** 删除 */
    @ApiOperation(value="删除")
    @PostMapping("/delete")
    public R delete(@RequestBody ArticleComment req, @RequestAttribute("userId") Integer userId){
        ArticleComment articleComment = articleCommentService.getById(req.getId());
        /*if (!userId.equals(articleComment.getReaderId())) {
            return R.error("只有本人才能删除");
        }*/
        articleCommentService.remove(req.getId());
        return R.ok();
    }
}